/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gouwudai.table.view.style2;

import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.ToolbarBuilder;
import org.extremecomponents.util.HtmlBuilder;

/**
 * @author Eccn-sunpauliz
 */
public class ExToolbarBuilder extends ToolbarBuilder {

    public ExToolbarBuilder(TableModel model) {
        super( model);
    }
    
    public ExToolbarBuilder(HtmlBuilder html, TableModel model) {
        super(html,model);
    }
    
    public void rowsDisplayed(){
    	getHtmlBuilder().newline();
        getHtmlBuilder().tabs(4);
        rowsDisplayedDroplist();
        getHtmlBuilder().img();
        getHtmlBuilder().src(BuilderUtils.getImage(getTableModel(), BuilderConstants.TOOLBAR_ROWS_DISPLAYED_IMAGE));
        getHtmlBuilder().style("border:0");
        getHtmlBuilder().alt(getMessages().getMessage(ExBuilderConstants.TOOLBAR_ROW_DISPLAYED_TOOLTIP));        
        getHtmlBuilder().xclose();
    }
    
    public void gotoPages(){
    	getHtmlBuilder().newline();
        getHtmlBuilder().tabs(4);
        gotoPagesDroplist();
        getHtmlBuilder().img();
        getHtmlBuilder().src(BuilderUtils.getImage(getTableModel(), ExBuilderConstants.TOOLBAR_GOTO_PAGES_IMAGE));
        getHtmlBuilder().style("border:0");
        getHtmlBuilder().alt(getMessages().getMessage(ExBuilderConstants.TOOLBAR_GOTO_PAGES_TOOLTIP));        
        getHtmlBuilder().xclose();
    }
    
    public void gotoPagesDroplist() {
        int currentRowsDisplayed = getTableModel().getLimit().getCurrentRowsDisplayed();
        int currentPage = getTableModel().getLimit().getPage();
        int ys = 0;
		int totalPages = 0;
		int totalRows = getTableModel().getLimit().getTotalRows();
		if (currentRowsDisplayed > 0) {
			ys = totalRows % currentRowsDisplayed;
			totalPages = ys == 0 ? totalRows / currentRowsDisplayed : totalRows / currentRowsDisplayed + 1;
		} else {
			totalPages = 1;
			currentPage = 1;
		}
		
        getHtmlBuilder().select().name(getTableModel().getTableHandler().prefixWithTableId() + ExTableConstants.PAGES);

        StringBuffer onchange = new StringBuffer();
        onchange.append(new ExTableActions(getTableModel()).getGotoPagesAction());
        getHtmlBuilder().onchange(onchange.toString());

        getHtmlBuilder().close();

        getHtmlBuilder().newline();
        getHtmlBuilder().tabs(4);

        // all pages
        int count = 1;
		if (totalPages > 1) {
			for (; count <= totalPages; ++count) {
				getHtmlBuilder().option().value(String.valueOf(count));
				if (count == currentPage) {
					getHtmlBuilder().selected();
				}
				getHtmlBuilder().close();
				getHtmlBuilder().append(String.valueOf(count));
				getHtmlBuilder().optionEnd();
			}
		}

        getHtmlBuilder().newline();
        getHtmlBuilder().tabs(4);
        getHtmlBuilder().selectEnd();
    }

}
